import tkinter as tk
from tkinter import scrolledtext

def generate_perfect_aligned_receipt():
    """生成每列完全对齐的购物小票文本"""
    # 商品信息
    items = [
        {"name": "金士顿U盘8G", "quantity": 1, "unit_price": 40.00},
        {"name": "胜创16GTF卡", "quantity": 1, "unit_price": 50.00},
        {"name": "读卡器", "quantity": 1, "unit_price": 8.00},
        {"name": "网线2米", "quantity": 1, "unit_price": 5.00},
        {"name": "鼠标", "quantity": 1, "unit_price": 100}
    ]

    # 计算数据
    total_quantity = sum(item["quantity"] for item in items)
    total_amount = sum(item["quantity"] * item["unit_price"] for item in items)
    discount_rate = 0.8
    discount_amount = total_amount * (1 - discount_rate)
    discounted_total = total_amount * discount_rate

    # 定义列宽，确保对齐
    name_width = 14
    quantity_width = 6
    price_width = 8
    amount_width = 8

    # 构建小票内容（精确控制每列宽度）
    receipt = "=" * 40 + "\n"
    receipt += f"{'数码配件超市':^40}\n"
    receipt += f"{'联系电话:123456':^40}\n"
    receipt += "=" * 40 + "\n"
    receipt += f"单号: DH202311010001{'':<15}\n"
    receipt += f"时间: 2023-11-01 20:56:15{'':<8}\n"
    receipt += "=" * 40 + "\n"
    
    # 表头
    receipt += f"{'名称':<{name_width}}{'数量':<{quantity_width}}"
    receipt += f"{'单价':<{price_width}}{'金额':<{amount_width}}\n"
    receipt += "-" * 40 + "\n"
    
    # 商品列表（精确对齐）
    for item in items:
        amount = item["quantity"] * item["unit_price"]
        receipt += f"{item['name']:<{name_width}}"
        receipt += f"{item['quantity']:<{quantity_width}}"
        receipt += f"{item['unit_price']:<{price_width}.2f}"
        receipt += f"{amount:<{amount_width}.2f}\n"
    
    receipt += "-" * 40 + "\n"
    receipt += f"总数: {total_quantity:>{35 - len('总数: ')}}\n"
    receipt += f"总额: {total_amount:>{35 - len('总额: ')}.2f}\n"
    receipt += f"折扣: {discount_rate*10:.0f}折{''*(35 - len('折扣: 8折'))}\n"
    receipt += f"折扣金额: {discount_amount:>{35 - len('折扣金额: ')}.2f}\n"
    receipt += f"折后总额: {discounted_total:>{35 - len('折后总额: ')}.2f}\n"
    receipt += f"实收: {discounted_total:>{35 - len('实收: ')}.2f}\n"
    receipt += f"找零: 0.00{''*(35 - len('找零: 0.00'))}\n"
    receipt += "=" * 40 + "\n"
    receipt += f"收银: 管理员{'':<24}\n"
    receipt += f"感谢光临，欢迎下次再来！{'':<14}\n"
    receipt += "=" * 40 + "\n"
    
    return receipt

def create_aligned_receipt_window():
    """创建显示完全对齐购物小票的窗口"""
    # 创建主窗口
    window = tk.Tk()
    window.title("购物小票")
    window.geometry("420x500")  # 适合内容的窗口大小
    
    # 创建带滚动条的文本区域，使用等宽字体确保排版正确
    text_widget = scrolledtext.ScrolledText(
        window,
        font=("Consolas", 10),  # 等宽字体保证对齐
        wrap=tk.NONE,  # 不自动换行，保持横向对齐
        width=45,
        height=28,
        bg="white"
    )
    text_widget.pack(
        padx=10,
        pady=10,
        fill=tk.BOTH,
        expand=True
    )
    
    # 插入小票内容
    receipt_content = generate_perfect_aligned_receipt()
    text_widget.insert(tk.INSERT, receipt_content)
    
    # 设置为只读模式
    text_widget.config(state=tk.DISABLED)
    
    # 运行窗口主循环
    window.mainloop()

if __name__ == "__main__":
    create_aligned_receipt_window()
    